# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
import codecs
from HunterHouse.items import HunterSellHouseItem, HunterRentHouseItem, HunterCommunityHouseItem

class HunterhousePipeline(object):
    # 调用 scrapy 提供的 json exporter 导出 json 文件
    def __init__(self):
        self.sellfile = codecs.open(u'南京二手房房源具体信息.json', 'w', encoding='utf-8')
        self.rentfile = codecs.open(u'南京租房房源具体信息.json', 'w', encoding='utf-8')
        self.communityfile = codecs.open(u'南京小区具体信息.json', 'w', encoding='utf-8')

    def close_spider(self, spider):
        self.sellfile.close()
        self.rentfile.close()
        self.communityfile.close()

    def process_item(self, item, spider):
        if item.__class__ == HunterSellHouseItem:
            lines = json.dumps(dict(item), ensure_ascii=False) + '\n'
            self.sellfile.write(lines)
        elif item.__class__ == HunterRentHouseItem:
            lines = json.dumps(dict(item), ensure_ascii=False) + '\n'
            self.rentfile.write(lines)
        elif item.__class__ == HunterCommunityHouseItem:
            lines = json.dumps(dict(item), ensure_ascii=False) + '\n'
            self.communityfile.write(lines)
        return item
